<html>
<head>
    <title>Web Socket Test</title>
</head>
<body>
<script type="text/javascript">
    var socket;
    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:9999/ws");
        socket.onmessage = function (event) {
            var ta = document.getElementById('responseText');
            ta.value += '\n' + (new Date().toLocaleString()) + '\n' + event.data
        };
        socket.onopen = function (event) {
            var ta = document.getElementById('responseText');
            ta.value = "会话连接成功";
        };
        socket.onclose = function (event) {
            var ta = document.getElementById('responseText');
            ta.value += "\n会话已关闭";
        };
    } else {
        alert("此浏览器不支持，请更换浏览器。");
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState === WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("会话还未连接，请刷新页面重试");
        }
    }
</script>
<form onsubmit="return false;">
    <input type="text" name="message" value="Hello, World!"><input
        type="button" value="Send Web Socket Data"
        onclick="send(this.form.message.value)">
    <h3>Output</h3>
    <textarea id="responseText" style="width: 500px; height: 300px;"></textarea>
</form>
</body>
</html>  